﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
    <%@ include file="../system/index/meta.jsp" %>
    <title></title>
    <meta name="keywords" content="H-ui.admin v3.1,H-ui网站后台模版,后台模版下载,后台管理系统模版,HTML后台模版下载">
    <meta name="description" content="H-ui.admin v3.1，是一款由国人开发的轻量级扁平化网站后台模板，完全免费开源的网站后台管理系统模版，适合中小型CMS后台系统。">
</head>
<body>
<article class="page-container">
	<form method="post" class="form form-horizontal" id="form">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>新闻标题：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="" placeholder="请输入新闻标题:" id="title" name="title">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>新闻内容：</label>
			<div class="formControls col-xs-8 col-sm-9">
			     <div id="editor">
			        <p>新闻内容</p>
			      </div>
			</div>
		</div>
		<div class="row cl">
	        <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>是否发布：</label>
	        <div class="formControls col-xs-8 col-sm-9 skin-minimal">
	            <div class="radio-box">
	                <input name="state" type="radio" value="0" checked />
	                <label for="state-1">草稿</label>
	            </div>
	            <div class="radio-box">
	                <input name="state" type="radio" value="1" />
	                <label for="state-2">发布</label>
	            </div>
	        </div>
	    </div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<button type="button" class="btn btn-success radius" onclick="save();"><i class="icon-ok"></i>保存新闻</button>
			</div>
		</div>
	</form>
</article>

<!--_footer 作为公共模版分离出去-->
<%@ include file="../system/index/footer.jsp" %>
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="${pageContext.request.contextPath}/js/plugins/jquery.validation/jquery.validate.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/plugins/jquery.validation/validate-methods.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/plugins/jquery.validation/messages_zh.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/plugins/wangEditor/wangEditor.min.js"></script>
<script type="text/javascript">
	$('.skin-minimal input').iCheck({
        checkboxClass: 'icheckbox-blue',
        radioClass: 'iradio-blue',
        increaseArea: '20%'
    });
	
	var E = window.wangEditor;
    var editor = new E('#editor');
    editor.create();
    
    function save() {
    	var title = $("#title").val();
    	var content = editor.txt.html();
    	var state = $("input[type='radio']:checked").val();
    	
    	if(title.trim().length == 0) {
    		layer.msg("新闻标题不能为空!", {
    	        icon: 5,
    	        offset: '100px',
    	        time: 1500 //2秒关闭（如果不配置，默认是3秒）
    	    });
    		return false;
    	}
    	if(editor.txt.text().trim().length == 0) {
            layer.msg("新闻内容不能为空!", {
                icon: 5,
                offset: '100px',
                time: 1500 //2秒关闭（如果不配置，默认是3秒）
            });
            return false;
        }
    	var pageContext = "${pageContext.request.contextPath}";
    	$.ajax({
    	    url:pageContext+"/newsServlet?method=save",    //请求的url地址
    	    dataType:"json",   //返回格式为json
    	    async:true,//请求是否异步，默认为异步，这也是ajax重要特性
    	    data:{"title":title, "content":content, "state":state},    //参数值
    	    type:"POST",   //请求方式
    	    success:function(req){
    	    	//请求成功时处理
                if(req.success) {
                    layer.msg(req.msg, {
                          icon: 6,
                          offset: '100px',
                          time: 1500 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        location.href=pageContext+"/newsServlet?method=list";
                    });
                } else {
                    layer.msg(req.msg, {
                          icon: 5,
                          offset: '100px',
                          time: 1500 //2秒关闭（如果不配置，默认是3秒）
                    }, function() {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }); 
                }
    	    },
    	    error:function(){
    	        //请求出错处理
    	    }
    	});
    }
    
    
    







</script>
</body>
</html>